<?php
class Model_User extends Core_Database
{
    /**
     * kiem tra username ton tai trong csdl
     * true: neu ton tai
     * false: nguoc lai
     * @param : username
     * @return : bool
     * */
    public function checkUsername($username)
    {
        $sql = 'select user_id from users where username = ?';
        $sth = $this->_db->prepare($sql);
        $sth->bindParam(1, $username,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetchAll();
        if(count($result) == 0)
        	return false;
       return true; 	
    }
    
    /**
     * kiem tra email ton tai
     * true: neu ton tai
     * false: nguoc lai
     * @param : email
     * @return : bool
     * */
    public function checkEmail($email)
    {
        $sql = 'select user_id from users where email = ?';
        $sth = $this->_db->prepare($sql);
        $sth->bindParam(1, $email,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetchAll();
        if(count($result) == 0)
        	return false;
       return true; 	
    }
    
    /**
     * Them tai khoan
     * true : thanh cong
     * false: that bai
     * 
     * @param :username password fullname email user_dir
     * @return :bool 
     */
    public function insertUser($username,$password,$fullname,$email,$user_dir)
    {
    	$sql = 'insert into users(username,password,fullname,email,user_dir) values (:username,PASSWORD(:password),:fullname,:email,:user_dir)';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':username', $username, PDO::PARAM_STR);
    	$sth->bindParam(':password', $password, PDO::PARAM_STR);
    	$sth->bindParam(':fullname', $fullname, PDO::PARAM_STR);
    	$sth->bindParam(':email', $email, PDO::PARAM_STR);
    	$sth->bindParam(':user_dir', $user_dir, PDO::PARAM_STR);
    	return $sth->execute();
    }
    
    /**
     * Kiem tra dang nhap
     * 
     * @param : username password
     * @return: array(user_id,fullname) : neu ton tai    null : neu khong ton tai
     * */
   public function login($username,$password)
   {
     $sql = 'select user_id, fullname from users where username = :username and password = PASSWORD(:password) and ban = 0 ';
     $sth = $this->_db->prepare($sql);
     $sth->bindParam(':username',$username,PDO::PARAM_STR);
     $sth->bindParam(':password',$password,PDO::PARAM_STR);
     $status = $sth->execute();
     if (!$status)
          return null;
     $result = $sth->fetchAll();
     if(count($result)>0)
        return $result;
     else
        return null;     
   }  
   /**
    * Lay thong tin cua mot user
    * 
    * @param : user_id
    * @return : array : neu ton tai  null : neu khong ton tai
    * 
    * */
    public function getUser($user_id)
    {
     $sql = 'select * from users where user_id = :user_id';
     $sth = $this->_db->prepare($sql);
     $sth->bindParam(':user_id',$user_id,PDO::PARAM_INT);
     $status = $sth->execute();
     if (!$status)
          return null;
     $result = $sth->fetchAll();
     if(count($result)>0)
        return $result;
     else
        return null;     
    }
    /**
     * Doi thong tin ho ten cua user
     * @param : user_id  fullname
     * @return : true : thanh cong , false: that bai
     * */
     public function updateFullname($user_id,$fullname)
     {
        $sql = 'update users set fullname = :fullname where user_id = :user_id';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':user_id',$user_id,PDO::PARAM_INT);
  	    $sth->bindParam(':fullname', $fullname, PDO::PARAM_STR);
    	return $sth->execute();
     }
     /**
      * Doi mat khau
      * @param : User_id old_password password
      * @return: 0: mat khau cu sai, 1: that bai, 2: thanh cong
      * 
      * */
      public function changePassword($user_id,$old_password,$password)
      {
        $sql = 'select user_id from users where user_id = :user_id and password = PASSWORD(:old_password)';
        $sth = $this->_db->prepare($sql);
    	$sth->bindParam(':user_id',$user_id,PDO::PARAM_INT);
  	    $sth->bindParam(':old_password', $old_password, PDO::PARAM_STR);
    	if(! $sth->execute())
            return 1;
        if(count($sth->fetchAll())==0)
            return 0;
            
        $sql = 'update users set password = PASSWORD(:password) where user_id = :user_id';
        $sth = $this->_db->prepare($sql);
        $sth->bindParam(':user_id',$user_id,PDO::PARAM_INT);
  	    $sth->bindParam(':password', $password, PDO::PARAM_STR);
    	if(! $sth->execute())
            return 1;
        else
            return 2;
      }
      
      /**
       * 
       * Lay so tien trong tai khoan nguoi dung
       * @param $user_id
       * @return interger credit; 0 : neu khong ton tai
       */
      public function getCredit($user_id)
      {
      	$sql = 'select credit from users where user_id = :user_id';
     	$sth = $this->_db->prepare($sql);
     	$sth->bindParam(':user_id',$user_id,PDO::PARAM_INT);
     	$status = $sth->execute();
     	if (!$status)
        	  return 0;
     	$result = $sth->fetchAll();
     	if(count($result)>0)
        	return $result[0]['credit'];
     	else
        	return 0;     
      }
      
      /**
       * 
       * Cap nhat so tien trong tai khoan
       * @param $credit
       * @param $user_id
       * @return bool true:thanh cong ; false: that bai
       */
      public function updateCredit($credit,$user_id)
      {
      	$sql = 'update users set credit = :credit where user_id = :user_id';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':user_id',$user_id,PDO::PARAM_INT);
  	    $sth->bindParam(':credit', $credit, PDO::PARAM_INT);
    	return $sth->execute();
      }
}